/*
https://leetcode.cn/problems/find-k-closest-elements/submissions/570844086/
找到k个最接近的元素
二分查找
*/

class Solution {
public:
    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        int l = 0;
        int r = arr.size() - k;
        while(l < r)
        {
            int m = (l + r) / 2;
            if(x - arr[m] > arr[m + k] - x)
            {
                l = m + 1;
            }
            else
            {
                r = m;
            }
        }
        // 返回从low开始的k长度的数组
        return vector<int>(arr.begin() + l, arr.begin() + k + l);
    }
};